package com.linktel.productionLine.service;

import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.dao.*;
import com.linktel.productionLine.entity.CommitUserProductionLine;
import com.linktel.productionLine.entity.dto.userProductionLine.RelationDTO;
import com.linktel.productionLine.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 对 提交用户_产线关联关系 的操作
 *
 * @Author HuangJiaHui
 * @Date 2023/8/25 10:26
 */
@Service
@AllArgsConstructor
public class CommitUserProductionLineService {

    private CommitUserProductionLineDao commitUserProductionLineDao;
    private MonthlyPlanningDao monthlyPlanningDao;
    private DailyPlanningDao dailyPlanningDao;
    private DailyAttendanceDao dailyAttendanceDao;
    private ProductionLineDao productionLineDao;

    /**
     * 添加用户和产线关联关系
     * 一条产线只能关联一个用户
     *
     * @param commitUserId 用户id
     * @param productionLineCode 产线id
     * @param currentUserId 当前登录用户id
     * @return
     */
    public Boolean addRelation(Long commitUserId, String productionLineCode, Long currentUserId) {
        Long userProductionLineId = commitUserProductionLineDao.getByProductionLineId(productionLineCode);
        if (userProductionLineId != null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "一条产线只能由一个人负责，不能重复添加关联");
        }
        return commitUserProductionLineDao.addRelation(commitUserId, productionLineCode, currentUserId);
    }

    /**
     * 查询所有提交用户_产线关联关系
     *
     * @return 提交用户_产线关联关系列表
     */
    public List<RelationDTO> listRelationByUserId(Long commitUserId) {
        return commitUserProductionLineDao.listRelationByUserId(commitUserId);
    }

    /**
     * 删除提交用户_产线关联关系
     * 同时根据该关联的用户id和产线id，删除月度规划、日度规划
     *
     * @param id 提交用户_产线关联关系id
     * @param currentUserId 当前登录用户id
     * @return 是否删除成功
     */
    public Boolean deleteRelation(Long id, Long currentUserId) {
        CommitUserProductionLine commitUserProductionLine = commitUserProductionLineDao.getByCommitUserId(id);
        if (commitUserProductionLine != null) {
            Long commitUserId = commitUserProductionLine.getCommitUserId();
            String productionLineCode = commitUserProductionLine.getProductionLineCode();
            monthlyPlanningDao.deleteMonthlyPlanningByRelation(commitUserId, productionLineCode, currentUserId);
            dailyPlanningDao.deleteDailyPlanningByRelation(commitUserId, productionLineCode, currentUserId);
            dailyAttendanceDao.deleteDailyAttendanceByRelation(commitUserId, productionLineCode, currentUserId);
        }
        return commitUserProductionLineDao.deleteRelation(id, currentUserId);
    }

    /**
     * 删除所有该用户关联的产线信息
     * 同时根据删除用户id，删除月度规划、日度规划
     *
     * @param commitUserId 用户id
     * @param currentUserId 当前登录用户id
     * @return 是否删除成功
     */
    public Boolean deleteRelationByCommitUserId(Long commitUserId, Long currentUserId) {
        monthlyPlanningDao.deleteMonthlyPlanningByUserId(commitUserId, currentUserId);
        dailyPlanningDao.deleteDailyPlanningByUserId(commitUserId, currentUserId);
        dailyAttendanceDao.deleteDailyAttendanceByUserId(commitUserId, currentUserId);
        return commitUserProductionLineDao.deleteRelationByCommitUserId(commitUserId, currentUserId);
    }
}
